/**
 * Copyright 2007-2015 Arthur Blake
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.log4jdbc;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * RDBMS specifics for the Oracle DB.
 *
 * @author Arthur Blake
 */
public class OracleRdbmsSpecifics extends DefaultRdbmsSpecifics
{

	private DateFormatPatternHolder dateFormatPatternHolder = new DateFormatPatternHolder("MM/dd/yyyy HH:mm:ss.SSS", "mm/dd/yyyy hh24:mi:ss.ff3",
			"MM/dd/yyyy HH:mm:ss", "mm/dd/yyyy hh24:mi:ss");

	public OracleRdbmsSpecifics()
  {
    super();
  }

	public OracleRdbmsSpecifics(DateFormatPatternHolder dateFormatPatternHolder)
	{
		this();
		this.dateFormatPatternHolder = dateFormatPatternHolder;
	}

	@Override
  public String formatParameterObject(Object object)
  {
    if (object instanceof Timestamp)
    {
      return "to_timestamp('" + new SimpleDateFormat(dateFormatPatternHolder.javaDateFormat).
        format(object) + "', '" + dateFormatPatternHolder.oracleDateFormat + "')";
    }
    else if (object instanceof Date)
    {
      return "to_date('" + new SimpleDateFormat(dateFormatPatternHolder.javaTimestampFormat).
			  format(object) + "', '" + dateFormatPatternHolder.oracleTimestampFormat + "')";
    }
    else
    {
      return super.formatParameterObject(object);
    }
  }

	/**
	 * Date patterns holder for Java/Oracle conversion and display.
	 *
	 * @author Saad Benbouzid
	 */
	public static final class DateFormatPatternHolder {

		/** Date pattern for {@link Date} conversion */
		private final String javaDateFormat;
		/** Oracle pattern for {@link Date} conversion */
		private final String oracleDateFormat;

		/** Date pattern for {@link Timestamp} conversion */
		private final String javaTimestampFormat;
		/** Oracle pattern for {@link Timestamp} conversion */
		private final String oracleTimestampFormat;

		public DateFormatPatternHolder(String javaDateFormat, String oracleDateFormat, String javaTimestampFormat,
				String oracleTimestampFormat) {
			this.javaDateFormat = javaDateFormat;
			this.oracleDateFormat = oracleDateFormat;
			this.javaTimestampFormat = javaTimestampFormat;
			this.oracleTimestampFormat = oracleTimestampFormat;
		}
	}
}